package com.xingchen.springboot3.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xingchen.springboot3.common.bean.PageBean;
import com.xingchen.springboot3.mapper.ArticleMapper;
import com.xingchen.springboot3.pojo.dto.ArticleDto;
import com.xingchen.springboot3.pojo.entity.Article;
import com.xingchen.springboot3.service.ArticleService;
import com.xingchen.springboot3.utils.ThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @author weijingchen
 * @DateTime 2025-09-26
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ArticleServiceImpl implements ArticleService {
    private final ArticleMapper articleMapper;

    @Override
    public void add(ArticleDto dto) {
        Article article = BeanUtil.copyProperties(dto, Article.class);
        article.setCreateUser(ThreadLocalUtil.getId());
        log.info("添加文章：{}", article);
        articleMapper.add(article);
    }

    @Override
    public void update(ArticleDto dto) {
        Article article = BeanUtil.copyProperties(dto, Article.class);
        log.info("修改文章：{}", article);
        articleMapper.update(article);
    }

    @Override
    public PageBean<Article> list(ArticleDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<Article> list = articleMapper.list(dto);
        return new PageBean<>(list.getTotal(), list.getResult());
    }

    @Override
    public void delete(Integer id) {
        log.info("删除文章：{}", id);
        articleMapper.delete(id);
    }

    @Override
    public Article getById(Integer id) {
        return articleMapper.getById(id);
    }
}
